<template>
  <el-dialog
    title="提示"
    :visible="showPersonDialog"
    width="40%"
    @close="$emit('update:showPersonDialog', false)"
  >
    <el-form ref="form" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="人员名称 :" prop="userName">
        <el-input
          v-model="form.userName"
          style="width: 400px"
          placeholder="请输入"
          maxlength="5"
          show-word-limit
        />
      </el-form-item>
      <el-form-item label="角色 :" prop="roleId">
        <el-select
          v-model="form.roleId"
          placeholder="请选择"
          style="width: 400px"
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="联系电话 :" prop="mobile">
        <el-input
          v-model="form.mobile"
          style="width: 400px"
          placeholder="请输入"
          maxlength="11"
          show-word-limit
        />
      </el-form-item>

      <el-form-item label="负责区域 :" prop="regionName">
        <el-select
          v-model="form.regionId"
          placeholder="请选择"
          style="width: 400px"
        >
          <!-- //这里是有问题的 -->
          <el-option
            v-for="item in areaData"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <!-- //上传头像 -->
      <el-form-item label="头像 :">
        <el-upload
          class="avatar-uploader"
          action="https://jsonplaceholder.typicode.com/posts/"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon" />
        </el-upload>
        <span class="picText"> 支持扩展名：jpg、png，文件不得大于100kb </span>
      </el-form-item>
      <!-- //状态 -->
      <el-form-item label="状态 :">
        <el-checkbox v-model="form.status" label="是否启用" />
      </el-form-item>
      <el-form-item>
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-button @click="isDone">取消</el-button>
            <el-button type="primary" @click="submit">确定</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
//
import { areaList, addPerson, updatePerson, PersonDetail } from '@/api/personnel/personnel'
export default {
  name: 'AddPersonDialog',
  props: {
    showPersonDialog: {
      type: Boolean
    },
    personnelList: {
      type: Array,
      default: null
    },
    id: {
      type: Number,
      default: null
    },
    type: {
      type: String,
      required: true
    },
    perDetail: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      form: {
        userName: '', //	用户名
        roleId: '', // 角色id
        mobile: '', // 手机号
        regionId: '', // 所属区域Id
        regionName: '', // 所属区域名称
        status: '' // 启用状态
      },
      rules: {
        userName: [{ required: true, message: '请输入', trigger: 'blur' }],
        roleId: [{ required: true, message: '请输入', trigger: 'blur' }],
        mobile: [{ required: true, message: '请输入', trigger: 'blur' }],
        regionId: [{ required: true, message: '请输入', trigger: 'blur' }]

      },
      options: [
        {
          value: '1',
          label: '正式'
        },
        {
          value: '2',
          label: '非正式'
        }
      ],
      imageUrl: '', // 上传头像
      areaValue: '',
      areaData: [],
      queryObj: {
        pageIndex: 1,
        pageSize: 10000
      }

    }
  },
  async created() {
    const data = await areaList(this.queryObj)
    this.areaData = data.currentPageRecords
  },
  methods: {
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    areaName(area) {
      console.log(area)
    },
    async submit() {
      const data = this.areaData.find((item) => item.id === this.form.regionId)
      this.form.regionName = data.name
      try {
        this.$refs.form.validate()
        if (this.type === 'edit') {
          console.log(1)
          console.log(this.id)
          await updatePerson(this.form, this.id)
          this.$emit('update:showPersonDialog', false)
          this.$emit('getPersonData')
        } else {
          await addPerson(this.form)
          this.$emit('getPersonData')
          this.$emit('update:showPersonDialog', false)
        }
      } catch (error) {
        console.log(error)
      }
    },
    async getPersonDetail() {
      const data = await PersonDetail(this.id)
      this.form = { ...data }
    },
    // 取消按钮
    isDone() {
      this.$refs.form.resetFields()
      this.form = {
        userName: '', //	用户名
        roleId: '', // 角色id
        mobile: '', // 手机号
        regionId: '', // 所属区域Id
        regionName: '', // 所属区域名称
        status: '' // 启用状态
      }

      this.showPersonDialog = false
    }
  }
}
</script>

<style lang="scss">

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  background-color: #f3f6fb;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 84px;
  height: 84px;
  line-height: 84px;
  text-align: center;
}
.avatar {
  width: 84px;
  height: 84px;
  display: block;
}
.picText {
  font-size: 14px;
  color: #bac0cd;
}
.el-form-item__label {
  font-size: 14px;
  color: #606266;
  font-weight: normal;
}
</style>
